package org.jeecg.modules.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.entity.*;
import org.jeecg.modules.business.enums.AgreementOrderStatusEnum;
import org.jeecg.modules.business.enums.FeeSubjectType;
import org.jeecg.modules.business.enums.FeeType;
import org.jeecg.modules.business.mapper.BusOrderFeeMapper;
import org.jeecg.modules.business.service.*;
import org.jeecg.modules.business.vo.BusOrderFeeGoodsVo;
import org.jeecg.modules.business.vo.RoomOrderFeeVo;
import org.jeecg.modules.kc.entity.KcDepositoryInGoods;
import org.jeecg.modules.rooms.Enum.ChargeTypeEnum;
import org.jeecg.modules.rooms.Enum.ReadTypeEnum;
import org.jeecg.modules.rooms.entity.*;
import org.jeecg.modules.rooms.service.*;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: bus_order_fee
 * @Author: jeecg-boot
 * @Date:   2023-04-08
 * @Version: V1.0
 */
@Service
public class BusOrderFeeServiceImpl extends ServiceImpl<BusOrderFeeMapper, BusOrderFee> implements IBusOrderFeeService {

    @Resource
    IBusRoomPayTypeService payTypeService;
    @Resource
    IBusMemberCardService memberCardService;
    @Resource
    IBusMemberBalanceLogService memberBalanceLogService;
    @Resource
    IBusOrderFeeGoodsService orderFeeGoodsService;
    @Resource
    CesGoodsServiceImpl goodsService;
    @Resource
    BusMarketMarketAgreementUnitServiceImpl marketAgreementUnitService;
    @Resource
    IBusAgreementOrderFeeService agreementOrderFeeService;
    @Resource
    IBusRoomsLivingOrderService roomsLivingOrderService;
    @Resource
    CesRoomLayoutServiceImpl layoutService;
    @Resource
    private IBusLivingLayoutDayPriceService livingLayoutDayPriceService;
    @Resource
    private ICesHouseLongRentSchemeService longRentSchemeService;
    @Resource
    private CesRoomsServiceImpl roomsService;
    @Resource
    ICesHouseLongRentMeterLogService houseLongRentMeterLogService;


    @Override
    public Boolean strikeBalance(List<BusOrderFee> busOrderFeeList) {
        List<String> ids = busOrderFeeList.stream().map(BusOrderFee::getId).collect(Collectors.toList());
        // 查询账单
        List<BusOrderFee> findBusOrderFees = listByIds(ids);
        if (findBusOrderFees.isEmpty()){
            throw new JeecgBootException("未找到订单");
        }
        List<BusOrderFee> orderFeeList = new ArrayList<>();
        busOrderFeeList.forEach(orderFee ->{
            Optional<BusOrderFee> findFee = findBusOrderFees.stream().filter(fee -> fee.getId().equals(orderFee.getId())).findFirst();
            findFee.ifPresent(e -> {
                if (ObjectUtil.equals(e.getPreferentialStatus(),2)){
                    throw new JeecgBootException("结算的账单不能冲账，请先退款再冲账");
                }
                // 冲账单
                BusOrderFee newOrderFee = BeanUtil.copyProperties(e, BusOrderFee.class);
                newOrderFee.setReturnFeeId(e.getId());
                newOrderFee.setReturnItem(true);
                newOrderFee.setId(String.valueOf(IdWorker.getId()));
                newOrderFee.setCreateTime(new Date());
                newOrderFee.setSubjectType(FeeSubjectType.TUI_DAN_JIE_ZHANG_SHOU_KUAN.getKey());
                newOrderFee.setPayTime(new Date());
                newOrderFee.setDayTime(new Date());
                newOrderFee.setRemark(orderFee.getRemark());
                newOrderFee.setReturnNum(orderFee.getReturnNum());
                newOrderFee.setCustorerOrderRemark(orderFee.getCustorerOrderRemark());
                // orderFee.getMoney()前端需要传入为负数的差价
                newOrderFee.setMoney(orderFee.getMoney());
                // 冲账退款分开操作，不需要了
                // 冲账时如果勾选退款，将冲账单设置为已结算，生成未结算且为负的收款单
//                if (orderFee.getFeeType().equals(FeeType.CONSUME.getKey()) && orderFee.getReturnItem()){
//                    if (orderFee.getPreferentialStatus() == 1){
//                        throw new JeecgBootException("未结账的订单不能退款");
//                    }
//                    newOrderFee.setPreferentialStatus(2);
//                    // TODO: 2023/10/12 负金额的收款单
//                    BusOrderFee retrunOrderFee = BeanUtil.copyProperties(newOrderFee, BusOrderFee.class);;
//                    retrunOrderFee.setConsumeFeeId(orderFee.getId());
//                    retrunOrderFee.setRemark("冲账产生退款单");
//                    retrunOrderFee.setPreferentialStatus(2);
//                    retrunOrderFee.setFeeType(2);
//                    retrunOrderFee.setReturnItem(null);
//                    retrunOrderFee.setReturnFeeId(null);
//                    orderFeeList.add(retrunOrderFee);
//                }
                // 商品冲账
                if (FeeSubjectType.SHANG_PIN.getKey().equals(e.getSubjectType())){
                    List<BusOrderFeeGoods> orderFeeGoods = orderFeeGoodsService.list(Wrappers.<BusOrderFeeGoods>lambdaQuery().eq(BusOrderFeeGoods::getOrderFeeId, e.getId()));
                    if (orderFeeGoods.size() != 1){
                        throw new JeecgBootException("查找费用商品详情失败");
                    }
                    // 前端需要传入需要冲掉的数量
                    orderFeeGoods.get(0).setNum(orderFee.getReturnNum());
                    orderFeeGoods.get(0).setOrderFeeId(newOrderFee.getId());
                    // 添加仓库销售退货,修改商品库存
                    KcDepositoryInGoods depositoryInGood = addDepositoryInGood(orderFeeGoods.get(0));
                    goodsService.saleReturn(depositoryInGood,orderFee.getId());
                }
                orderFeeList.add(newOrderFee);
            });

        });
        return saveBatch(orderFeeList);
    }

    public KcDepositoryInGoods addDepositoryInGood(BusOrderFeeGoods feeGood){
        KcDepositoryInGoods depositoryInGoods = new KcDepositoryInGoods();
        depositoryInGoods.setGoodsId(feeGood.getGoodsId());
        depositoryInGoods.setNum(feeGood.getNum());
        depositoryInGoods.setFeeId(feeGood.getOrderFeeId());
        depositoryInGoods.setHotelId(feeGood.getHotelId());
        depositoryInGoods.setCreateTime(new Date());
        return depositoryInGoods;
    }

    @Override
    public Boolean refundBatch(List<BusOrderFee> busOrderFeeList, String hotelId) {
        List<BusRoomPayType> payTypeList = payTypeService.list(Wrappers.<BusRoomPayType>lambdaQuery().eq(BusRoomPayType::getHotelId, hotelId));
        Optional<BusRoomPayType> cardPayType = payTypeList.stream().filter(e -> "会员卡".equals(e.getName())).findFirst();
        cardPayType.ifPresent(e -> {
            // 会员支付的订单
            List<BusOrderFee> busOrderFees = busOrderFeeList.stream().filter(fee -> e.getId()
                            .equals(fee.getPayType())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(busOrderFees)){
                List<String> FeeIds = busOrderFees.stream().map(BusOrderFee::getId).collect(Collectors.toList());
                List<BusMemberBalanceLog> balanceLogList = memberBalanceLogService.list(Wrappers.<BusMemberBalanceLog>lambdaQuery().in(BusMemberBalanceLog::getOrderFeeId, FeeIds));
                List<String> memberId = balanceLogList.stream().map(BusMemberBalanceLog::getMemberId).distinct().collect(Collectors.toList());
                List<BusMemberCard> memberCardList = memberCardService.list(Wrappers.<BusMemberCard>lambdaQuery().in(BusMemberCard::getId, memberId));
                busOrderFees.forEach(fee -> {
                    List<BusMemberBalanceLog> balanceLog = balanceLogList.stream().filter(log -> fee.getReturnFeeId()
                            .equals(log.getOrderFeeId())).collect(Collectors.toList());
                    if (balanceLog.size() != 1){
                        throw new JeecgBootException("账单未找到对应的会员支付扣款记录");
                    }
                    Optional<BusMemberCard> optional = memberCardList.stream().filter(card -> card.getId().equals(balanceLog.get(0).getMemberId())).findFirst();
                    optional.ifPresent(card -> {
                        balanceLog.get(0).setWithhold(balanceLog.get(0).getWithhold().subtract(fee.getMoney()));
                        balanceLog.get(0).setWithhold(balanceLog.get(0).getBalance().add(fee.getMoney()));
                        card.setBalance(card.getBalance().add(fee.getMoney()));
                    });
                });
                memberBalanceLogService.updateBatchById(balanceLogList);
                memberCardService.updateBatchById(memberCardList);
            }
        });
        Optional<BusRoomPayType> agreementPayType = payTypeList.stream().filter(e -> "单位挂账".equals(e.getName())).findFirst();
        agreementPayType.ifPresent(e -> {
            List<BusOrderFee> busOrderFees = busOrderFeeList.stream().filter(fee -> e.getId()
                    .equals(fee.getPayType())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(busOrderFees)){
                List<String> agreementUnitIds = busOrderFees.stream().map(BusOrderFee::getAgreementUnitId).distinct().collect(Collectors.toList());
                List<BusMarketAgreementUnit> marketAgreementUnitList = marketAgreementUnitService.listByIds(agreementUnitIds);
                if (CollUtil.isEmpty(agreementUnitIds) || agreementUnitIds.size() != marketAgreementUnitList.size()){
                    throw new JeecgBootException("协议单位匹配异常");
                }
                List<BusAgreementOrderFee> agreementOrderFeeList = new ArrayList<>();
                busOrderFees.forEach(fee ->{
                    marketAgreementUnitList.stream().filter(agreement -> agreement.getId().equals(fee.getAgreementUnitId())).findFirst().ifPresent(agreement ->{
                        // 计算已用额度与当前退款的差价
                        BigDecimal differMoney = agreement.getAmountUsed().subtract(fee.getMoney());
                        if (differMoney.compareTo(new BigDecimal(0)) < 0 ){
                            agreement.setAmountUsed(new BigDecimal(0));
                            agreement.setBalance(agreement.getBalance().subtract(differMoney));
                        } else {
                            agreement.setAmountUsed(differMoney);
                        }
                        BusAgreementOrderFee agreementOrderFee = marketAgreementUnitService.addAgreementOrderFee(fee.getId(), fee.getMoney(), agreement.getId(), false);
                        agreementOrderFeeList.add(agreementOrderFee);
                    });
                });
                marketAgreementUnitService.updateBatchById(marketAgreementUnitList);
                agreementOrderFeeService.saveBatch(agreementOrderFeeList);
            }
        });
        busOrderFeeList.forEach(e -> {
            e.setId(null);
            // 将扣款变成负数金额
            e.setMoney(e.getMoney().multiply(BigDecimal.valueOf(-1)));
        });
        return saveBatch(busOrderFeeList);
    }


    @Override
    public Boolean collectionBatch(List<BusOrderFee> busOrderFeeList, String hotelId) {
        if (CollUtil.isEmpty(busOrderFeeList)){
            return true;
        }
        List<BusRoomPayType> payTypeList = payTypeService.list(Wrappers.<BusRoomPayType>lambdaQuery().eq(BusRoomPayType::getHotelId, hotelId));
        payTypeList.stream().filter(e -> "会员卡".equals(e.getName())).findFirst().ifPresent(type -> {
            //找出会员支付的账单，将会员支付的账单中的会员id去重
            List<BusOrderFee> memberPayFee = busOrderFeeList.stream().filter(fee -> type.getId().equals(fee.getPayType())).collect(Collectors.toList());
            List<String> cardIds = memberPayFee.stream().map(BusOrderFee::getVipCardId).filter(StrUtil::isNotEmpty).distinct().collect(Collectors.toList());
            if (memberPayFee.size() != cardIds.size()){
                throw new JeecgBootException("会员卡支付的账单需要标识会员卡");
            }
            if (CollUtil.isNotEmpty(cardIds)) {
                List<BusMemberCard> busMemberCards = memberCardService.listByIds(cardIds);
                if(CollUtil.isEmpty(busMemberCards) || cardIds.size() != busMemberCards.size()) {
                    throw new JeecgBootException("会员卡匹配异常");
                }
                List<BusMemberBalanceLog> busMemberBalanceLogs = new ArrayList<>();
                // 循环会员支付的账单，修改对应的会员卡余额，添加会员支付日志
                memberPayFee.forEach(fee -> {
                    fee.setId(String.valueOf(IdWorker.getId()));
                    if (StrUtil.isEmpty(fee.getVipCardId())){
                        throw new JeecgBootException("支付方式为会员但没有会员id");
                    }
                    BusMemberCard busMemberCard = busMemberCards.stream().filter(card -> card.getId().equals(fee.getVipCardId())).findFirst().get();
                    // 会员卡扣款, 产生会员消费记录
                    BusMemberBalanceLog balanceLog = memberCardService.cardPayFee(busMemberCard, fee.getMoney(), fee.getId(), hotelId);
                    balanceLog.setWithhold(fee.getMoney());
                    busMemberBalanceLogs.add(balanceLog);
                });
                memberCardService.updateBatchById(busMemberCards);
                memberBalanceLogService.saveBatch(busMemberBalanceLogs);
            }
        });
        payTypeList.stream().filter(e -> "单位挂账".equals(e.getName())).findFirst().ifPresent(type -> {
            //找出单位挂账支付的账单，将单位挂账支付的账单中的单位挂账id去重
            List<BusOrderFee> agreementUnitPayFee = busOrderFeeList.stream()
                    .filter(fee -> type.getId().equals(fee.getPayType())).collect(Collectors.toList());
            List<String> agreementUnitIds = agreementUnitPayFee.stream().map(BusOrderFee::getAgreementUnitId)
                    .filter(StrUtil::isNotEmpty).distinct().collect(Collectors.toList());
            if (agreementUnitPayFee.size() != agreementUnitIds.size()){
                throw new JeecgBootException("会员卡支付的账单需要标识会员卡");
            }
            if (CollUtil.isNotEmpty(agreementUnitIds)){
                List<BusMarketAgreementUnit> marketAgreementUnitList = marketAgreementUnitService.listByIds(agreementUnitIds);
                List<BusAgreementOrderFee> agreementOrderFeeList = new ArrayList<>();
                if (agreementUnitIds.size() != marketAgreementUnitList.size()){
                    throw new JeecgBootException("单位挂账匹配异常");
                }
                agreementUnitPayFee.forEach(fee -> {
                    fee.setId(String.valueOf(IdWorker.getId()));
                    // 找出账单匹配的合同单位
                    BusMarketAgreementUnit marketAgreementUnit = marketAgreementUnitList.stream().filter(agreement ->
                            agreement.getId().equals(fee.getAgreementUnitId())).findFirst().get();
                    // 单位合同扣款或挂账, 产生消费记录
                    BusAgreementOrderFee agreementOrderFee = marketAgreementUnitService.agreementPayFee(marketAgreementUnit, fee.getMoney(), fee.getId(), hotelId);
                    agreementOrderFeeList.add(agreementOrderFee);
                });
                marketAgreementUnitService.updateBatchById(marketAgreementUnitList);
                agreementOrderFeeService.saveBatch(agreementOrderFeeList);
            }

        });
        return saveBatch(busOrderFeeList);
    }


//    public BusAgreementOrderFee addAgreementOrderFee(String feeId, BigDecimal money, String agreeUnitId, Boolean isCollect){
//        LoginUser user = TokenUtils.getAuthUser();
//        BusAgreementOrderFee agreementOrderFee = new BusAgreementOrderFee();
//        agreementOrderFee.setAgreementUnitId(agreeUnitId);
//        agreementOrderFee.setCreateUserId(user.getId());
//        agreementOrderFee.setOrderFeeId(feeId);
//        //生成订单号
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//        agreementOrderFee.setOrderNo("R"+sdf.format(System.currentTimeMillis()));
//        agreementOrderFee.setStatus(AgreementOrderStatusEnum.NOT_SETTLEMENT.getKey());
//        // 商品消费
//        agreementOrderFee.setType(3);
//        if (isCollect){
//            agreementOrderFee.setPayPrice(money);
//            agreementOrderFee.setProjectName("收款");
//        } else {
//            agreementOrderFee.setProjectName("退款");
//            agreementOrderFee.setPayPrice(money.multiply(new BigDecimal(-1)));
//        }
//        agreementOrderFee.setCreatedTime(new DateTime());
//        return agreementOrderFee;
//    }

    @Override
    public IPage<RoomOrderFeeVo> strikeBalancePage(Page<RoomOrderFeeVo> page, String roomName, Integer preferentialStatus,String bookingOrderId, DateTime startTime, DateTime endTime) {
        List<String> livingOrderIds = null;
        if(StrUtil.isNotEmpty(bookingOrderId)){
            LambdaQueryWrapper<BusRoomsLivingOrder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(BusRoomsLivingOrder::getId).eq(BusRoomsLivingOrder::getBookingOrderId,bookingOrderId);
            List<BusRoomsLivingOrder> livingOrders = roomsLivingOrderService.list(queryWrapper);
            livingOrderIds = livingOrders.stream().map(BusRoomsLivingOrder::getId).collect(Collectors.toList());
        }

        IPage<RoomOrderFeeVo> feePage = baseMapper.strikeBalancePage(page,roomName, preferentialStatus,livingOrderIds, startTime, endTime);
        List<String> ids = feePage.getRecords().stream().filter(e -> FeeSubjectType.SHANG_PIN.getKey().equals(e.getSubjectType()))
                .map(RoomOrderFeeVo::getReturnFeeId).collect(Collectors.toList());
        if (!ids.isEmpty()){
            List<BusOrderFeeGoodsVo> busOrderFeeGoodsVos = orderFeeGoodsService.orderGoodDetailByFeeId(ids);
            feePage.getRecords().forEach(e -> busOrderFeeGoodsVos.stream().filter(feeGood -> StrUtil.equals(feeGood.getOrderFeeId(),e.getReturnFeeId())).findFirst().ifPresent(e::setFeeGoodVo));
        }
        return feePage;
    }

    @Override
    public void addBillFee(BusRoomsLivingOrder livingOrder, CesRooms room, CesHouseLongRentScheme longRentScheme,List<BusLivingLayoutDayPrice> livingRoomDayPrices) {
        if (livingOrder.getLivingType() != 5){
            BigDecimal money = new BigDecimal(0);
            Optional<BusLivingLayoutDayPrice> dayPrice = livingRoomDayPrices.stream().filter(e -> room.getId().equals(e.getRoomId())
                    && (DateUtils.formatDate(new Date())).equals(DateUtils.formatDate(e.getDayTime()))).findFirst();
            if (dayPrice.isPresent()){
                money = dayPrice.get().getPrice();
            } else {
                CesRoomLayout layout = layoutService.getById(room.getLayoutId());
                if(layout == null) throw new JeecgBootException("房价不存在");
                money = layout.getMarketPrice();
            }
            BusOrderFee dayOrderFee = new BusOrderFee();
            dayOrderFee.setDayTime(new Date());
            dayOrderFee.setCreateTime(new Date());
            dayOrderFee.setMoney(money);
            dayOrderFee.setFeeType(1);
            dayOrderFee.setSubjectType(FeeSubjectType.MEI_RI_FANG_FEI.getKey());
            dayOrderFee.setRoomId(room.getId());
            dayOrderFee.setRemark(DateUtils.formatDate(new Date(), "MM-dd") +  " 房费");
            dayOrderFee.setLivingOrderId(livingOrder.getId());
            dayOrderFee.setHotelId(room.getHotelId());
            save(dayOrderFee);
        } else {
            longRentSchemeService.addLivingScheme(longRentScheme, livingOrder.getId());
            longRentScheme.getHouseLongRentChargeList().forEach(e -> {
                if (ChargeTypeEnum.DISPOSABLE.getKey().equals(e.getChargeType()) && BooleanUtil.isFalse(e.getIsMust())
                        || ChargeTypeEnum.PERIODICITY.getKey().equals(e.getChargeType())){
                    BusOrderFee dayOrderFee = new BusOrderFee();
                    dayOrderFee.setDayTime(new Date());
                    dayOrderFee.setCreateTime(new Date());
                    dayOrderFee.setRemark(e.getName());
                    String roomName = room.getPrefix() != null? room.getPrefix():"";
                    roomName += roomName + room.getName();
                    dayOrderFee.setMoney(e.getMoney());
                    dayOrderFee.setSubjectType(FeeSubjectType.OTHER.getKey());
                    if (ChargeTypeEnum.PERIODICITY.getKey().equals(e.getChargeType())) {
                        dayOrderFee.setMoney(e.getMoney().multiply(BigDecimal.valueOf(e.getNum())));
                        if (e.getIsMust() != null && e.getIsMust()){
                            dayOrderFee.setSubjectType(FeeSubjectType.MEI_RI_FANG_FEI.getKey());
                            dayOrderFee.setRemark(DateUtil.format(new Date(), "yyyy-MM") + e.getName());
                        }
                    }
                    dayOrderFee.setFeeType(1);
                    dayOrderFee.setRoomId(room.getId());
                    dayOrderFee.setLivingOrderId(livingOrder.getId());
                    dayOrderFee.setHotelId(room.getHotelId());
                    save(dayOrderFee);
                }
            });
        }

    }

    @Override
    public Boolean addMeterFee(List<CesHouseLongRentCharge> longRentChargeList) {
        if (CollUtil.isEmpty(longRentChargeList)){
            return true;
        }
        CesRooms room = roomsService.getOne(Wrappers.<CesRooms>lambdaQuery()
                .eq(CesRooms::getLivingOrderId, longRentChargeList.get(0).getLivingOrderId()).last("limit 1"));
        List<BusOrderFee> orderFeeList = new ArrayList<>();
        List<CesHouseLongRentMeterLog> longRentMeterLogList = new ArrayList<>();
        longRentChargeList.forEach(e -> {
            if (ChargeTypeEnum.WATER.getKey().equals(e.getChargeType())){
                BigDecimal money = e.getMoney().multiply(e.getMeterRead().subtract(room.getWaterMeter()));
                BusOrderFee orderFee = addFee(room, money, e);
                CesHouseLongRentMeterLog meterLog = addMeterLog(orderFee, room, e);
                room.setWaterMeter(e.getMeterRead());
                orderFeeList.add(orderFee);
                longRentMeterLogList.add(meterLog);
            } else if (ChargeTypeEnum.ELECTRIC.getKey().equals(e.getChargeType())){
                BigDecimal money = e.getMoney().multiply(e.getMeterRead().subtract(room.getEnergyMeter()));
                room.setEnergyMeter(e.getMeterRead());
                BusOrderFee orderFee = addFee(room, money, e);
                CesHouseLongRentMeterLog meterLog = addMeterLog(orderFee, room, e);
                orderFeeList.add(orderFee);
                longRentMeterLogList.add(meterLog);
            } else if (ChargeTypeEnum.GAS.getKey().equals(e.getChargeType())){
                BigDecimal money = e.getMoney().multiply(e.getMeterRead().subtract(room.getGasMeter()));
                room.setGasMeter(e.getMeterRead());
                BusOrderFee orderFee = addFee(room, money, e);
                CesHouseLongRentMeterLog meterLog = addMeterLog(orderFee, room, e);
                orderFeeList.add(orderFee);
                longRentMeterLogList.add(meterLog);
            }
        });
        roomsService.updateById(room);
        saveBatch(orderFeeList);
        houseLongRentMeterLogService.saveBatch(longRentMeterLogList);
        return true;
    }

    public BusOrderFee addFee(CesRooms room,BigDecimal money, CesHouseLongRentCharge charge){
        BusOrderFee orderFee = new BusOrderFee();
        orderFee.setId(String.valueOf(IdWorker.getId()));
        orderFee.setMoney(money);
        orderFee.setFeeType(1);
        orderFee.setSubjectType(FeeSubjectType.SHUI_DIAN_MEI.getKey());
        orderFee.setRemark(charge.getName());
        orderFee.setLivingOrderId(room.getLivingOrderId());
        orderFee.setPreferentialStatus(1);
        orderFee.setDayTime(new Date());
        orderFee.setRoomId(room.getId());
        orderFee.setHotelId(room.getHotelId());
        return orderFee;
    }

    public CesHouseLongRentMeterLog addMeterLog(BusOrderFee orderFee,CesRooms room, CesHouseLongRentCharge charge){
        CesHouseLongRentMeterLog meterLog = new CesHouseLongRentMeterLog();
        meterLog.setRoomId(room.getId());
        meterLog.setOrderFeeId(orderFee.getId());
        meterLog.setPrice(charge.getMoney());
        meterLog.setReadType(ReadTypeEnum.WATER.getKey());
        meterLog.setStartRead(room.getWaterMeter());
        meterLog.setEndRead(charge.getMeterRead());
        return meterLog;
    }
}
